#include <vector>
#include <algorithm>

template <class T> class PriorityStack
{
  private:
    std::vector<T> values;

  public:
    void push(T value)
    {
        values.push_back(value);
        int newIndex = values.size() - 1;
        while (newIndex >= 0)
        {
            int parentIndex = (newIndex - 1) / 2;
            if (values[newIndex] > values[parentIndex])
            {
                std::swap(values[newIndex], values[parentIndex]);
            }
            newIndex = parentIndex;
        }
    }
    void pop()
    {
        values[0] = values[values.size() - 1];
        values.pop_back();
        int current = 0;
        while (current < values.size())
        {
            int left = current * 2 + 1;
            int right = current * 2 + 2;
            int max = current;
            if (left < values.size() && values[left] > values[current])
            {
                max = left;
            }
            if (right < values.size() && values[right] > values[current])
            {
                max = right;
            }
            swap(values[current], values[max]);
            current = max;
        }
    }
    int size() { return values.size(); }
    std::vector<T> TopK(int k)
    {
        std::vector<T> result;
        for (int i = 0; i < k; i++)
        {
            result.push_back(values[i]);
        }
        return result;
    }

    T Top() { return values.front(); }
};